<?php
if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class Admin extends CI_Controller {

	public function index() {
		$data['page'] = 'home';
		$this -> load -> view('admin/main', $data);
	}

	function grab() {
		/**
		 * ada 3 step
		 * 1. input keyword, kategori
		 * 2. select gambar dan insert
		 * 
		 */
		$data['page'] = 'grab';
		$this -> load -> view('admin/main', $data);
	}

	function posts() {
		$data['page'] = 'posts';
		$this -> load -> view('admin/main', $data);
	}

	public function logout() {
		$this -> session -> sess_destroy();
		redirect('/');
	}
	
	function dograb($keyword='',$limit=0)
	{
		$jumlah = $limit;
		$q = $keyword;
		$qa = str_replace(' ', '%20', $q);
		$images=array();
		$k=0;
		$start=1;
		for ($i = 1; $i <= (int)($jumlah / 8); $i++) {
			$url = 'http://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=' . $qa . '&imgcolor=black&imgsz=xxlarge&rsz=8&start=' . $start;
			$start=($i*8)+1;
			//echo $url.'</br>';
			$json = file_get_contents($url);
			$json_res = json_decode($json, TRUE);
			//echo '<pre>';
			//print_r($json_res);
			//echo '</pre>';
			//exit();
			foreach ($json_res['responseData']['results'] as $key => $res) {
				//echo '<a href="' . $res['url'] . '">' . $res['url'] . '</a>' . '</br>';
				$images[$k]['url']=$res['url'];
				$images[$k]['tbUrl']=$res['tbUrl'];
				$images[$k]['width']=$res['width'];
				$images[$k]['height']=$res['height'];
				
				//$this -> copyimage($res['url']);
				//$image[$jumlah]['url']=$res['url'];
				//$jumlah++;
				$k++;
			}
		}
		//exit();
		return $images;
	}
	
	function selectitem()
	{
		$keyword=$this->input->post('keyword');
		$limit=$this->input->post('limit');
		$data['images']=$this->dograb($keyword,$limit);
		$data['page'] = 'grab-selectimage';
		
		$this -> load -> view('admin/main', $data);
	}

}
